//
//  ST_DateExentension.swift
//  btvideo
//
//  Created by macbookpro on 2024/2/6.
//  Copyright © 2024 JarvanZhang. All rights reserved.
//

import Foundation
extension Date{
    
    /**
     时间显示规则
     param: anchorDate 锚点 默认当前时间
     */
    func b_t_time_describe(anchorDate:Date = Date())->String{
        
        
        var text = ""
        
//        同一天
        if self.dateString(ofStyle: DateFormatter.Style.short) == anchorDate.dateString(ofStyle: DateFormatter.Style.short){
            
            
            let hours = self.hoursSince(anchorDate).abs.int
            
//            一小时内
            if hours < 1{
                
               let minutes = self.minutesSince(anchorDate).abs.int
                
//                一分钟内
               if minutes < 1{
                   text = "刚刚"
               }
//               超过一分钟
               else{
                   text = minutes.string + "分钟前"
               }
            }
//                超过一小时
            else{
                text =  hours.string + "小时前"
            }
        }else if anchorDate.yesterday.dateString(ofStyle: DateFormatter.Style.short) == self.dateString(ofStyle: DateFormatter.Style.short){
            
            text =  "昨天"
        }
//        一周内
        else if self.daysSince(anchorDate).abs.int < 7{
            
//            text = self.dayName(ofStyle: DayNameStyle.full)
//            let format = "EEEE"
//
            
            let dateFormatter = DateFormatter()
            
            dateFormatter.dateFormat = "EEEE"
            
            dateFormatter.locale = Locale.init(identifier: "zh_Hans_CN")
            
            text = dateFormatter.string(from: self)
            
//            print("本地星期几bug:",dateFormatter.locale ?? "","-----:",text)
            
            
        }else{
            
            text =  self.string(withFormat: "yyyy/MM/dd")
        }
        
        /*
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "EEEE"
        dateFormatter.locale = Locale.init(identifier: "zh_Hans_CN")
        let kkk = dateFormatter.string(from: self)
        print("本地星期几bug:",dateFormatter.locale,"-----:",kkk)
        */
        
        
        
        return text
        
    }
    
    
}
